package StudyCode.summary.mycollection;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class ReturnsASortedArrayFromTheMinimumToTheMaximumValue {
	public static void main(String[] argv) {
		double[] v = new double[]{34.45, 35.45, 36.67, 37.78, 37.0000,
				37.1234, 67.2344, 68.34534, 69.87700};
		System.out.println(java.util.Arrays.toString(sortMinToMax(v)));
	}

	public static double[] sortMinToMax(double[] v) {
		double[] ans = copy(v);
		quickSortMinToMax(ans, 0, ans.length - 1);
		return (ans);
	}

	public static String toString(double[] array) {

		StringBuffer buf = new StringBuffer(array.length);
		int i;
		for (i = 0; i < array.length - 1; i++) {
			buf.append(array[i]);
			buf.append(',');
		}
		buf.append(array[i]);
		return buf.toString();
	}

	public static String toString(double[][] array) {
		StringBuffer buf = new StringBuffer();
		for (int k = 0; k < array.length; k++) {
			buf.append(toString(array[k]));
			buf.append(System.getProperty("line.separator"));
		}
		return buf.toString();
	}

	public static String toString(int[] array) {
		StringBuffer buf = new StringBuffer(array.length);
		int i;
		for (i = 0; i < array.length - 1; i++) {
			buf.append(array[i]);
			buf.append(',');
		}
		buf.append(array[i]);
		return buf.toString();
	}

	public static String toString(int[][] array) {
		StringBuffer buf = new StringBuffer();
		for (int k = 0; k < array.length; k++) {
			buf.append(toString(array[k]));
			buf.append(System.getProperty("line.separator"));
		}
		return buf.toString();
	}

	public static int[] copy(int[] array) {
		int[] result;
		result = new int[array.length];
		System.arraycopy(array, 0, result, 0, array.length);
		return result;
	}

	public static long[] copy(long[] array) {
		long[] result;
		result = new long[array.length];
		System.arraycopy(array, 0, result, 0, array.length);
		return result;
	}

	public static float[] copy(float[] array) {
		float[] result;
		result = new float[array.length];
		System.arraycopy(array, 0, result, 0, array.length);
		return result;
	}

	public static double[] copy(double[] array) {
		double[] result;
		result = new double[array.length];
		System.arraycopy(array, 0, result, 0, array.length);
		return result;
	}

	public static double[][] copy(double[][] v) {
		double[][] ans = new double[v.length][];
		for (int k = 0; k < v.length; k++)
			ans[k] = copy(v[k]);
		return (ans);
	}

	public static int[][] copy(int[][] v) {
		int[][] ans = new int[v.length][];
		for (int k = 0; k < v.length; k++)
			ans[k] = copy(v[k]);
		return (ans);
	}

	private static void quickSortMinToMax(double a[], int lo0, int hi0) {
		int lo = lo0;
		int hi = hi0;
		double mid;

		if (hi0 > lo0) {

			mid = a[(int) Math.round((lo0 + hi0) / 2.0)];

			while (lo <= hi) {

				while ((lo < hi0) && (a[lo] < mid))
					++lo;


				while ((hi > lo0) && (a[hi] > mid))
					--hi;

				if (lo <= hi) {
					swap(a, lo, hi);
					++lo;
					--hi;
				}
			}

			if (lo0 < hi)
				quickSortMinToMax(a, lo0, hi);


			if (lo < hi0)
				quickSortMinToMax(a, lo, hi0);

		}
	}

	private static void swap(final double a[], final int i, final int j) {
		final double T;
		T = a[i];
		a[i] = a[j];
		a[j] = T;

	}
}
